/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 */

import { Component } from '@ohos/sdkmanager-common';

import { SdkVersion } from '../../version/sdk-version.js';
import { SdkVersionEnum } from '../../version/sdk-version-enum.js';

/**
 * sdk公共实例
 *
 * @since 2021-12-16
 */
export class SdkComponent {
  private readonly _baseDir: string;
  private readonly _componentApiVersion: number;
  private readonly _componentVersion: SdkVersion;
  private readonly _releaseType: string;

  constructor(component: Component) {
    this._baseDir = component.getLocation()!;
    this._componentApiVersion = component.getFullApiVersion().getMajor();
    this._componentVersion = new SdkVersion(component.getVersion());
    this._releaseType = component.getReleaseType();
  }

  getBaseDir(): string {
    return this._baseDir;
  }

  getComponentApiVersion(): number {
    return this._componentApiVersion;
  }

  getComponentVersion(): SdkVersion {
    return this._componentVersion;
  }

  getReleaseType(): string {
    return this._releaseType;
  }

  /**
   * 不高于'3.2.2.5' 且 不是'3.2.1.4'版本的SDK, 要求配置UISyntax
   *
   * @returns {boolean}
   */
  requireUISyntax(): boolean {
    return !this._componentVersion.isHigherThan(SdkVersionEnum.REQUIRE_UI_SYNTAX) && !this._componentVersion.equals(SdkVersionEnum.SUPPORT_UI_SYNTAX);
  }

  /**
   * 支持资源增量编译的版本
   * ohos必须大于或等于3.2.7.2
   * 半容器版本必须大于或等于3.1.1.2
   *
   * @returns {boolean}
   */
  supportCompileResourceIncrement(isHarmonyOS: boolean): boolean {
    if (isHarmonyOS) {
      return (
        this._componentVersion.isHigherThan(SdkVersionEnum.HOS_SUPPORT_COMPILE_RESOURCE_INCREMENT) ||
        this._componentVersion.equals(SdkVersionEnum.HOS_SUPPORT_COMPILE_RESOURCE_INCREMENT)
      );
    } else {
      return (
        this._componentVersion.isHigherThan(SdkVersionEnum.OHOS_SUPPORT_COMPILE_RESOURCE_INCREMENT) ||
        this._componentVersion.equals(SdkVersionEnum.OHOS_SUPPORT_COMPILE_RESOURCE_INCREMENT)
      );
    }
  }

  /**
   * 支持Ohpm的SDK版本
   * ohos不能小于3.2.10.7
   * hos不能小于3.2.5.6
   */
  supportOhpmProject(isHarmonyOS: boolean): boolean {
    if (isHarmonyOS) {
      return (
        this._componentVersion.isHigherThan(SdkVersionEnum.HOS_SUPPORT_OHPM_PROJECT) || this._componentVersion.equals(SdkVersionEnum.HOS_SUPPORT_OHPM_PROJECT)
      );
    }
    return (
      this._componentVersion.isHigherThan(SdkVersionEnum.OHOS_SUPPORT_OHPM_PROJECT) || this._componentVersion.equals(SdkVersionEnum.OHOS_SUPPORT_OHPM_PROJECT)
    );
  }
}
